package org.magicsoft.office.excel.model;

import org.magicsoft.office.db.SQLType;
import org.magicsoft.office.exception.ModelException;

public class Column {

	// Excel列号。A, B, C等。
	private String columnNumber = null;

	// 该列对应的临时表字段名，一般是列号+下划线。A_, B_等。（加下划线是因为...... AS是数据库关键字......）
	private String tempTableColumn = null;

	// 字段类型，和jdbc的Types类型一一对应，名字也完全一样。
	private SQLType sqlType = null;

	// 临时表字段长度
	private int length = -1;

	// 临时表字段精度。
	private int precision = -1;

	// 该列在insert数据的时候，preparedStatement的参数索引。（这里未必和集合中的下标是一样的，因为有可能excel中间某几列是不进行导入的。）
	private int parameterIndex = 1;

	private String header = null;

	private TargetTable bindTable = null;

	private String bindColumn = null;

	private boolean mandatory = false;

	private Reference reference = null;

	// 前一列，后面逻辑处理需要，构造了一个列模型的链表。
	private Column prev = null;

	// 后一列，后面逻辑处理需要，构造了一个列模型链表。
	private Column next = null;

	public Column(String columnNumber) {
		this(columnNumber, SQLType.NVARCHAR, 255, 2);
	}

	public Column(String columnNumber, SQLType sqlType) {
		this(columnNumber, sqlType, 255, 2);

		if (sqlType == SQLType.CHAR || sqlType == SQLType.NCHAR) {
			throw new ModelException("请使用varchar或nvarchar替代。");
		}
	}

	public Column(String columnNumber, SQLType sqlType, int length) {
		this(columnNumber, sqlType, length, 2);
	}

	public Column(String columnNumber, SQLType sqlType, int length, int precision) {
		this.columnNumber = columnNumber;
		this.sqlType = sqlType;
		this.length = length;
		this.precision = precision;
	}

	public Column bind(String column) {
		this.bindColumn = column;
		return this;
	}

	public Column bind(String bindTableName, String column) {
		this.bindTable = new TargetTable(bindTableName);
		this.bindColumn = column;
		return this;
	}

	public boolean isMandatory() {
		return mandatory;
	}

	public Column setMandatory() {
		this.mandatory = true;
		return this;
	}

	public Reference getReference() {
		return reference;
	}

	public Column setReference(Reference reference) {
		this.reference = reference;
		reference.setColumn(this);
		return this;
	}

	public TargetTable getBindTable() {
		return bindTable;
	}

	public void setBindTable(TargetTable bindTable) {
		this.bindTable = bindTable;
	}

	public String getBindColumn() {
		return bindColumn;
	}

	public String getColumnNumber() {
		return columnNumber;
	}

	public String getTempTableColumn() {
		if (this.tempTableColumn == null) {
			this.tempTableColumn = this.getColumnNumber() + "_";
		}
		return tempTableColumn;
	}

	public int getParameterIndex() {
		return parameterIndex;
	}

	public void setParameterIndex(int parameterIndex) {
		this.parameterIndex = parameterIndex;
	}

	public void setColumnNumber(String columnNumber) {
		this.columnNumber = columnNumber;
	}

	public SQLType getSqlType() {
		return sqlType;
	}

	public void setSqlType(SQLType sqlType) {
		this.sqlType = sqlType;
	}

	public int getLength() {
		return length;
	}

	public void setLength(int length) {
		this.length = length;
	}

	public int getPrecision() {
		return precision;
	}

	public void setPrecision(int precision) {
		this.precision = precision;
	}

	public String getHeader() {
		return header;
	}

	public void setHeader(String header) {
		this.header = header;
	}

	public Column getPrev() {
		return prev;
	}

	public void setPrev(Column prev) {
		this.prev = prev;
	}

	public Column getNext() {
		return next;
	}

	public void setNext(Column next) {
		this.next = next;
	}

}
